package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayVo;
import cn.wolfcode.feign.AlipayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.IOrderPayService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AlipayFeignApi alipayFeignApi;
    @Autowired
    private IOrderPayService orderPayService;

    @Value("${pay.returnUrl}")
    private String returnUrl;

    @Value("${pay.notifyUrl}")
    private String notifyUrl;

    @GetMapping("/alipay2")
    public Result<Object> prepay(String orderNo) {

        // 查询订单
        OrderInfo orderInfo = orderInfoService.getById(orderNo);
        if (orderInfo == null) {
            throw new BusinessException(SeckillCodeMsg.ORDER_NOT_EXISTS_ERROR);
        }
        
        // 构建支付对象
        PayVo vo = orderPayService.buildPauRequest(orderInfo, notifyUrl, returnUrl);
        // 远程调用支付服务发起预支付请求
        return alipayFeignApi.prepay(vo);
    }

    @GetMapping("/alipay")
    public Result alipay(String orderNo, Integer type) {
        // 积分支付商品
        orderPayService.alipay(orderNo, type);
        return Result.success("积分支付成功");
    }

}
